<template>
	<view class="">
		<cu-custom bgColor="bg-black" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">快速咨询</block>
		</cu-custom>
		<view class="cu-form-group">
			<textarea maxlength="-1" @input="textareaAInput" placeholder="请输入您要咨询的问题"></textarea>
		</view>
		<view class="cu-bar bg-white">
			<view class="action">
				证据上传
			</view>
			<view class="action">
				{{imgList.length}}/4
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>
		<view class="grid col-3 padding-sm margin-top bg-white">
			<view v-for="(item,index) in checkbox" class="padding-xs" :key="index">
				<button class="cu-btn orange lg block" :class="item.checked?'bg-orange':'line-orange'" @tap="ChooseCheckbox"
				 :data-value="item.value"> {{item.name}}
					<view class="cu-tag sm round" :class="item.checked?'bg-white text-orange':'bg-orange'" v-if="item.hot">HOT</view>
				</button>
			</view>
			<view class="padding-xs">
				<input placeholder-class="text-orange" placeholder="手动输入" class="cu-btn orange lg block line-orange" type="number"
				 value="" v-model="askMoney" />
			</view>
		</view>
		<view class="cu-form-group">
			<view class="title" style="height: auto;">支付金额无论多少,都可使用平台在线咨询律师服务(金额越高排名越靠前)</view>
		</view>
		<!-- 	<view class="cu-form-group">
			<view class="title">追加<text class="text-price text-red" style="margin: 0 6upx;">10</text>元平台加急</view>
			<checkbox class='round yellow' :class="checked?'checked':''" :checked="checked?true:false" value="C"></checkbox>
		</view> -->

		<view class="sub_btn_box">
			<view class="sub_pic">
				<view class="com_pic">

					合计: ￥10.00
				</view>
				<view class="vip_pic">
					会员: ￥9.00
				</view>
			</view>
			<view class="btn-group">
				<button class="cu-btn bg-orange round shadow-blur" @click="handOpenVip">开通会员</button>
				<button class="cu-btn bg-red round shadow-blur" style="margin-left:20upx;" @click="handSub">立即发布</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checked: true,
				textareaAValue: '',
				askMoney: '',
				imgList: [],
				checkbox: [{
					value: 0,
					name: '免费',
					checked: true,
					hot: false,
				}, {
					value: 20,
					name: '20元',
					checked: false,
					hot: false,
				}, {
					value: 30,
					name: '30元',
					checked: false,
					hot: true,
				}, {
					value: 60,
					name: '60元',
					checked: false,
					hot: true,
				}, {
					value: 80,
					name: '80元',
					checked: false,
					hot: false,
				}],
				userMsg: {}
			};
		},
		mounted() {
			if (uni.getStorageSync('USER')) {
				this.userMsg = JSON.parse(uni.getStorageSync('USER'))
			}
		},
		methods: {
			handOpenVip() {
				uni.showToast({
					title: '尽请期待',
					icon: 'none'
				})
			},
			async handSub() {
				if(this.userMsg.identity==2){
					uni.showToast({
						title:'您已经是律师了,不可以发布咨询',
						icon:'none'
					})
					return
				}
				let image = ''
				this.askMoney = this.askMoney == '' ? 0 : this.askMoney
				if (this.imgList.length != 0) {
					this.imgList
					image = this.imgList.join(',')
				}
				if (this.textareaAValue == '') {
					this.print('请输入内容')
				} else {
					uni.showLoading({
						title: '加载中...',
						icon: 'none'
					})
					var timestamp = (new Date()).getTime()
					console.log(uni.getStorageSync('USER'), 'www')
					let userID = this.userMsg.id
					let newForm = {
						userId: userID,
						isFloor: 1,
						hotNum: 0,
						createTime: timestamp,
						content: this.textareaAValue,
						parentId: 0,
						image: image,
						askMoney: this.askMoney
					}

					let pages = getCurrentPages(); // 当前页面
					let beforePage = pages[pages.length - 2];
					this.http.addDetails(newForm).then(async res => {


						uni.hideLoading()
						if (this.askMoney == 0) {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/index/index'
								})
							}, 1000)
							return
						}

						console.log(res.data, '回帖')
						this.print(res.message)
						await this.subPay(res.data)



					})
				}

			},
			ChooseCheckbox(e) {
				let items = this.checkbox;
				let values = e.currentTarget.dataset.value;
				this.askMoney = e.currentTarget.dataset.value;

				for (let i = 0, lenI = items.length; i < lenI; ++i) {

					if (items[i].value == values) {
						items[i].checked = !items[i].checked;
						// break
					} else {
						items[i].checked = false
						console.log(items[i].checked, '===', values)
					}
				}
			},
			textareaAInput(e) {
				this.textareaAValue = e.detail.value
			},
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						this.http.uploadFile({
							filePath: res.tempFilePaths[0]
						}).then(res2 => {
							console.log(res2, 'res2res2res2res2')
							let newData = JSON.parse(res2.data)
							this.imgList.push(newData.message)
						})
						// if (this.imgList.length != 0) {
						// 	this.imgList = this.imgList.concat(res.tempFilePaths)
						// } else {
						// 	this.imgList = res.tempFilePaths
						// }
					}
				});
				console.log('上传结果222', this.imgList)
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '删除',
					content: '确定要删除吗？',
					cancelText: '再看看',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.sub_btn_box {
		width: 100%;
		height: 154upx;
		padding: 0 30upx;
		background: rgba(255, 255, 255, 1);
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: fixed;
		bottom: 0;
		box-sizing: border-box;

		.btns {
			display: flex;

		}

		.sub_pic {
			.com_pic {
				color: #323233;
				font-size: 32upx;
				font-weight: bold;
			}

			.vip_pic {
				color: #E02020;
				font-size: 30upx;
				margin-top: 12upx;
			}
		}
	}
</style>
